﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigureInstancePage.xaml.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the ConfigureInstancePage type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Input;

    using MySemanticSearch.View;
    using MySemanticSearch.ViewModel;

    /// <summary>
    /// Allows the user to add another SQL Server Instance
    /// </summary>
    public partial class ConfigureInstancePage : IInstanceView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigureInstancePage"/> class.
        /// </summary>
        public ConfigureInstancePage()
        {
            InitializeComponent();
            this.DataContext = new ConfigureInstancePageViewModel(this);
            this.MainGrid.KeyDown += this.KbShortCutPressed;
        }

        /// <summary>
        /// Handles the method to call after the dialog is closed
        /// </summary>
        public event EventHandler CloseDialogHandler;

        /// <summary>
        /// Invokes the callback method in the viewmodel to pass the new SQL Server Instance
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        public void InvokeOnClose(EventArgs e)
        {
            var handler = this.CloseDialogHandler;
            if (handler == null || e == null)
            {
                return;
            }

            this.DialogResult = true;
            handler(this, e);
        }

        /// <summary>
        /// Updates bindings when a key is pressed
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event arguments
        /// </param>
        private void KbShortCutPressed(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var binding = this.InstanceNameTextBox.GetBindingExpression(TextBox.TextProperty);
                if (binding != null)
                {
                    binding.UpdateSource();
                }

                ((ConfigureInstancePageViewModel)this.DataContext).AddSqlInstance();
            }

            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }
    }
}
